//
//  HMDownClassVC.m
//  优学荟管理系统
//
//  Created by 陈辉明 on 2017/12/25.
//  Copyright © 2017年 星道三好. All rights reserved.
//

#import "HMDownClassVC.h"
#import "HMOrdinaryCourseVC.h"

@interface HMDownClassVC () <UIScrollViewDelegate>

@property(nonatomic,strong) UIButton *currentButton;
@property (weak, nonatomic) IBOutlet UIButton *firstButton;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *linViewLeft;
@property (weak, nonatomic) IBOutlet UIView *lineView;
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;


@end

@implementation HMDownClassVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.firstButton.selected = YES;
    self.currentButton = self.firstButton;
    [self setUpChildrenVC];
    self.title = @"已下架课程";
    [self setNavigationItems];
}

-(void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:ColorRGB(255,255,255)}]; //设置标题颜色
    [self.navigationController.navigationBar setBackgroundImage:[LLTools createImageWithColor:ColorRGB(47, 204, 138)] forBarMetrics:(UIBarMetricsDefault)];//设置导航栏颜色

    [SVProgressHUD dismiss];
}


- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    
    [self.navigationController.navigationBar setBackgroundImage:[LLTools createImageWithColor:ColorRGB(255, 255, 255)] forBarMetrics:(UIBarMetricsDefault)];//设置导航栏颜色
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:ColorRGB(51,51,51)}];//设置标题颜色
}


-(void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    [self setUpScrollView];
}

//自定义导航栏左边和右边按钮
- (void)setNavigationItems {
    
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithTargat:self action:@selector(leftBarButtonItemAction) image:@"icon_back_student_sign" highImage:@"icon_back_student_sign"];
    
}


- (void)setUpChildrenVC {
    HMOrdinaryCourseVC *vc1 = [HMOrdinaryCourseVC new];
    HMOrdinaryCourseVC *vc2 = [HMOrdinaryCourseVC new];
    HMOrdinaryCourseVC *vc3 = [HMOrdinaryCourseVC new];
    vc1.type = @"1";
    vc2.type = @"10";
    vc3.type = @"12";
    [self addChildViewController:vc1];
    [self addChildViewController:vc2];
    [self addChildViewController:vc3];
}

- (void)leftBarButtonItemAction{
    [self.navigationController popViewControllerAnimated:YES];
}




- (void)setUpScrollView {
    
    self.scrollView.contentSize = CGSizeMake([UIScreen mainScreen].bounds.size.width *self.childViewControllers.count, 0);
    self.scrollView.contentOffset = CGPointMake(0, 0);
    self.scrollView.bounces = NO;
    self.scrollView.pagingEnabled = YES;
    self.scrollView.delegate = self;
    for (int i = 0 ; i < self.childViewControllers.count; i ++ ) {
        UIViewController *vc = self.childViewControllers[i];
        
        vc.view.frame = CGRectMake(self.scrollView.bounds.size.width *i, 0, self.scrollView.bounds.size.width, self.scrollView.bounds.size.height);
        [self.scrollView addSubview:vc.view];
    }
    
}

- (IBAction)didSelectButton:(UIButton *)sender {
    self.currentButton.selected = NO;
    sender.selected = YES;
    self.currentButton = sender;
    HMOrdinaryCourseVC *vc = self.childViewControllers[sender.tag - 1000];
    if (sender.tag - 1000 == 0) {//普通课程type:1
        vc.type = @"1";
    }else if (sender.tag - 1000 == 1){//加强课type:10
        vc.type = @"10";
    } else {//小当家type:12
        vc.type = @"12";
    }
    [vc.dataSource removeAllObjects];
    [vc.tableView reloadData];
    [vc setNetWork];
    [UIView animateWithDuration:0.3 animations:^{
        _linViewLeft.constant = (sender.tag - 1000) *sender.bounds.size.width ;
        [self.view layoutIfNeeded];
        self.scrollView.contentOffset =CGPointMake((sender.tag - 1000) *[UIScreen mainScreen].bounds.size.width, 0);
    } completion:^(BOOL finished) {
        
    }];
    NSLog(@"%ld",sender.tag);
    
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    CGFloat x = scrollView.contentOffset.x;
    int pageNum  =  x/[UIScreen mainScreen].bounds.size.width;
    [self didSelectButton:[self.view viewWithTag:1000 + pageNum]];
    NSLog(@"%d",pageNum);
}





@end
